package com.j283_5.wisdom_park.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j283_5.wisdom_park.common.result.ResultData;
import com.j283_5.wisdom_park.common.utils.HashMapUtils;
import com.j283_5.wisdom_park.mapper.*;
import com.j283_5.wisdom_park.pojo.*;
import com.j283_5.wisdom_park.service.PereeService;
import com.j283_5.wisdom_park.vo.contract_management.CollectionRecordVo;
import com.j283_5.wisdom_park.vo.contract_management.ContractTableVo;
import com.j283_5.wisdom_park.vo.contract_management.CustomerVo;
import com.j283_5.wisdom_park.vo.tenant_management.Collection_ContractVo;
import com.j283_5.wisdom_park.vo.tenant_management.InsertPereeVo;
import com.j283_5.wisdom_park.vo.tenant_management.PereeRoomTenVo;
import com.j283_5.wisdom_park.vo.tenant_management.Peree_CommentsVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 租户表 服务实现类
 * </p>
 *
 * @author laoda
 * @since 2022-04-28
 */
@Service
public class PereeServiceImpl implements PereeService {
    @Resource
    private PereeMapper mapper;
    @Resource
    private PersonMapper personMapper;
    @Resource
    private TenementMapper tenementMapper;
    @Resource
    private RoomMapper roomMapper;
    @Resource
    private CollectionRecordMapper collectionRecordMapper;
    @Resource
    private ContractTableMapper contractTableMapper;
    /**
     * 多添加查询租户信息
     * @param pereeCommentsVo
     * @return
     */
    @Override
    public Map<String,Object> findPeree_CommentsVo(Peree_CommentsVo pereeCommentsVo, Integer page, Integer limit) {
        Integer start=null;
        //总条数
        Long countRows=null;
        List<Peree_CommentsVo> list=null;
        if (page!=null&&limit!=null){
            start=(page-1)*limit;
        }
        countRows=mapper.findPeree_CommentsCount(pereeCommentsVo);
        //如果总条数大于0  才去查询数据
        if(countRows!=null && countRows>0){
            list=mapper.findPeree_CommentsVo(pereeCommentsVo,start,limit);
            return new HashMapUtils()
                    .addData("countRows",countRows)
                    .addData("listPeree",list)
                    .getMap();
        }else{
            return new HashMapUtils().getMap();
        }
    }

    /**
     * 根据id查询租户基本信息
     * @param perId
     * @return
     */
    @Override
    public ResultData findPereeInfoById(Integer perId) {
        List<Peree> pereeInfoList = mapper.findPereeInfoById(perId);
        return ResultData.ok().data(pereeInfoList);
    }

    /**
     * 根据租户id查询人员信息
     * @param person
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Map<String, Object> findPersonByPerId(Person person, Integer page, Integer limit) {
        Integer start=null;
        //总条数
        Long countRows=null;
        List<Person> list=null;
        if (page!=null&&limit!=null){
            start=(page-1)*limit;
        }
        countRows=mapper.findPersonByPerIdCount(person);
        //如果总条数大于0  才去查询数据
        if(countRows!=null && countRows>0){
            list=mapper.findPersonByPerId(person,start,limit);
            return new HashMapUtils()
                    .addData("countRows",countRows)
                    .addData("listPerson",list)
                    .getMap();
        }else{
            return new HashMapUtils().getMap();
        }
    }

    /**
     * 删除租户
     * @param perIdList
     * @return
     */
    @Override
    public ResultData DeletePeree(List<Integer> perIdList) {
        UpdateWrapper<Peree> PereeDevelopmentUpdateWrapper = new UpdateWrapper<>();
        PereeDevelopmentUpdateWrapper.set("per_delete",1);
        PereeDevelopmentUpdateWrapper.in("per_id",perIdList);
        int i = mapper.update(null, PereeDevelopmentUpdateWrapper);
        if(i > 0){
            return ResultData.ok().message("删除成功!");
        }
        return ResultData.error().message("删除失败！");
    }

    /**
     * 租户删除
     * @return
     */
    @Override
    public ResultData DeletePereePerson(List<Integer> personIdList) {
        UpdateWrapper<Person> PersonUpdateWrapper = new UpdateWrapper<>();
        PersonUpdateWrapper.set("person_delete",1);
        PersonUpdateWrapper.in("person_id",personIdList);
        int i = personMapper.update(null, PersonUpdateWrapper);
        if(i > 0){
            return ResultData.ok().message("删除成功!");
        }
        return ResultData.error().message("删除失败！");
    }

    /**
     * 查询租户合同信息
     * @param perId
     * @return
     */
    @Override
    public List<ContractTableVo> findPereeContract(Integer perId) {
        List<ContractTableVo> pereeContract = mapper.findPereeContract(perId);
        return pereeContract;
    }

    /**
     * 查询租户房间信息
     * @param perId
     * @return
     */
    @Override
    public List<ContractTableVo> findPereeRoom(Integer perId) {
        List<ContractTableVo> pereeRoom = mapper.findPereeRoom(perId);
        return pereeRoom;
    }

    /**
     * 根据租户id查询收款信息
     * @param perId
     * @return
     */
    @Override
    public List<CollectionRecordVo> findCollectionByPereeId(Integer perId) {
        List<CollectionRecordVo> collectionByPereeId = mapper.findCollectionByPereeId(perId);
        return collectionByPereeId;
    }


    /**
     * 新增租户
     * @param insertPereeVo
     * @return
     */
    @Override
    public ResultData insertPereePerson(InsertPereeVo insertPereeVo) {
        QueryWrapper<Room> roomQueryWrapper = new QueryWrapper<>();
        roomQueryWrapper.select("ten_Id").eq("room_id",insertPereeVo.getRoomId());

        List<Room> rooms = roomMapper.selectList(roomQueryWrapper);
        Room room = rooms.get(0);

        Peree peree=new Peree();
        Person person=new Person();
        ContractTable contractTable=new ContractTable();
        CollectionRecord collectionRecord=new CollectionRecord();

//        增加人员
        person.setPersonName(insertPereeVo.getPersonName());
        person.setPersonPhone(insertPereeVo.getPersonPhone());
        person.setPersonSex(insertPereeVo.getPersonSex());
        person.setPersonPhoto(insertPereeVo.getPersonPhoto());
        int i = personMapper.insert(person);
        Integer personId = person.getPersonId();
        System.out.println(room.getTenId()+insertPereeVo.getRoomId());
//          增加合同收款信息
        collectionRecord.setCollNum(insertPereeVo.getCollNum());
        collectionRecord.setCredentials(insertPereeVo.getCredentials());
        collectionRecordMapper.insert(collectionRecord);
        Integer collId = collectionRecord.getCollId();
        System.out.println(collId);
//          增加合同信息
        contractTable.setCollId(collId);
        contractTable.setContractSerial(insertPereeVo.getContractSerial());
        contractTable.setContractCredentials(insertPereeVo.getContractCredentials());
        contractTableMapper.insert(contractTable);
        Integer contractId = contractTable.getContractId();
        System.out.println(contractId);
////       增加租户信息
        peree.setPerName(insertPereeVo.getPerName());
        peree.setPerSocialcode(insertPereeVo.getPerSocialcode());
        peree.setPerIndustry(insertPereeVo.getPerIndustry());
        peree.setPerIsnew(insertPereeVo.getPerIsnew());
        peree.setPerLicense(insertPereeVo.getPerLicense());
        peree.setTenId(room.getTenId());
        peree.setRoomId(insertPereeVo.getRoomId());
        peree.setPersonId(personId);
        peree.setPerLarge(insertPereeVo.getPerLarge());
        peree.setPerListed(insertPereeVo.getPerListed());
        peree.setPerMoney(insertPereeVo.getPerMoney());
        peree.setPerValuation(insertPereeVo.getPerValuation());
        peree.setContractId(contractId);
        int insert = mapper.insert(peree);

        if (i>0){
            return  ResultData.ok().message("插入成功");
        }else {

        }return  ResultData.error().message("插入失败");

    }

    /**
     * 查询楼宇no
     * @return
     */
    @Override
    public ResultData findtenNo() {
        QueryWrapper<Tenement> tenementQueryWrapper = new QueryWrapper<>();
        tenementQueryWrapper.select("ten_Serialint");
        List<Tenement> tenements = tenementMapper.selectList(tenementQueryWrapper);
        return ResultData.ok().data(tenements);
    }

    /**
     * 进入租户信息新增界面
     * @return
     */
    @Override
    public ResultData findPereeRoomTenVo() {
        List<PereeRoomTenVo> pereeRoomTenVo = mapper.findPereeRoomTenVo();
        return ResultData.ok().data(pereeRoomTenVo);
    }

}
